import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:jetour_line/model/common_list_title_model.dart';
import 'package:jetour_line/my_colors.dart';
import 'package:jetour_line/network/aha_http.dart';
import 'package:jetour_line/network/jetour_http.dart';
import 'package:jetour_line/utils/extension_int.dart';
typedef SelIndexCallBack = void Function(int cate);
class CommonListHeadWidget extends StatefulWidget {
  bool isLine;
  final SelIndexCallBack callBack;
  final String title;
  CommonListHeadWidget({super.key, required this.callBack,this.isLine = false,this.title = "推荐"});

  @override
  State<CommonListHeadWidget> createState() => _CommonListHeadWidgetState();
}

class _CommonListHeadWidgetState extends State<CommonListHeadWidget> {
  List<CommonTitleListModel> list = [];
  int selIndex = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getList();
  }
  @override
  Widget build(BuildContext context) {
    return list.isNotEmpty? Container( padding: EdgeInsets.only(left: 20.px,top: 14.px,bottom: 16.px), width: double.infinity,height: 86.px,color: MyColors.white, child: ListView.builder(scrollDirection:Axis.horizontal , itemCount: list.length, itemBuilder: (context,index){
      return itemWidget(index);
    })):const SizedBox();
  }
  Widget itemWidget(int index){
    String tex = list[index].name;
    return Padding(
      padding: EdgeInsets.only(right: 16.px),
      child: ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(54.px)),
        child: GestureDetector(
          onTap: (){
            if(selIndex == index){
             return;
            }else{
              selIndex = index;
              widget.callBack(list[index].id);
            }
            setState(() {

            });
          },
          child: Container(
            alignment: Alignment.center,
            color: selIndex == index?MyColors.color28B6B6:MyColors.colorF1F1F1,
            height: 54.px,
            child: Padding(
              padding: EdgeInsets.only(left: 40.px,right: 40.px,),
              child: Text(tex,style: TextStyle(fontSize: 26.px,color: selIndex == index?MyColors.white:MyColors.color999999,),),
            ),
          ),
        ),
      ),
    );
  }
  void getList(){
    if(widget.isLine){
      AhaHttp().get(AhaRouter.ThemeList,).then((result) {
        for (int i = 0; i < result.length; i++) {
          list.add(CommonTitleListModel.fromJson(result[i]));
        }
        list.insert(0, CommonTitleListModel(id: 0,name: "推荐"));
        setState(() {

        });
      });
    }else{
      JetourHttp().get(JetourRouter.GuideCateList).then((result) {

        for (int i = 0; i < result.length; i++) {
          list.add(CommonTitleListModel.fromJson(result[i]));
        }
        list.insert(0, CommonTitleListModel(id: 0,name: widget.title));
        setState(() {

        });

      }).catchError((e) {});
    }

  }
}
